define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                    items: [

                    ],
                    memberCardDetail:{
                        new_user:0,
                        total_user:0,
                        view_user:0
                    },
                    store:{},
                    loading:0,
                    nodata:0,
                    showAddMemberCard:0

                    
                };
            },
            el: '#pageMemberCard',
            created: function() {
                this.fetchData();
            },

            methods: {
                fetchData: function() {
                    var _this = this;
                    this.getList();
                },
                addCard:function(){
                  app.show_popup($("#popup_choose_wx"));
                },
                pageShow:function(){
                  var that = this;
                    var reload = app.getPageData("memberCard","reload");
                    if(reload == 1){
                       app.setPageData("memberCard",{reload:0});
                       that.getList();
                    }
                },

                more:function(e){
                  // console.log(_this);
                  var _this = $(e.target);
                  var that = this;
                  var id = _this.attr("data-id");
                  var appid = _this.attr("data-appid");
                  var card_id = _this.attr("data-card_id");
                  var params=[
                  {
                      'text':'充值/积分/等级',
                      'methods':function(){
                          app.colseHandle();
                          var user_info = app.getUserInfo();
                          window.location.href=app.params.apiDomain+"mall-admin/mall_promotion/index?from=webapp&from_wx_app=1&view=1&token="+user_info.token;
                          
                      }
                    },
                    {
                      'text':'修改',
                      'methods':function(){
                           app.colseHandle();
                           that.$router.push({ name: 'addMemberCard', params: { id: card_id,appid:appid } });
                      }
                    },
                    {
                      'text':'删除',
                      'methods':function(){
                           that.delete(card_id,_this);
                           
                      }
                    }
                    
                  ];
                  app.handle(params);

                     // _this.$router.push({ name: 'addMemberCard', params: { id: _this.cardInfo. } });
                     // { name: 'addMemberCard', params: { id: cardInfo.id ,appid:0 }}
                },
                delete:function(card_id,_this){
                  var that = this;
                  app.confirm({
                     'text':'删除卡券后，该卡券对应已生成的领取用二维码、添加到卡包JS API均会失效。确认删除吗？',
                     'yes':function(){
                        app.get("apiv2/card/delete",{card_id:card_id},function(res){
                            if(res.code!=200){
                               app.alert(res.msg);
                               return ;
                            }
                            _this.parents(".item").parent().hide();
                            that.showAddMemberCard =1;


                      });
                     }
                  });
                   
                },

                getMemberCarddetail:function(){
                  var memberCardDetail = {};
                    for(var i in  this.items){
                         var card_id = this.items[i].cardId;
                         var that = this;
                         app.get("apiv2/card/getMemberCarddetail", {card_id:card_id},function(res){
                            if(res.code==200){
                               memberCardDetail= res.data;
                               // console.log(memberCardDetail);
                               that.memberCardDetail = memberCardDetail;
                            }
                           
                        });
                    }
                    // var items = that.items ;
                    //  that.items =[];
                     // console.log(memberCardDetail);
                     

                },
               
                getList:function(){
                    var that = this;
                    if( that.loading == 1){
                        return ;
                     }
                     that.loading = 1;
                     
                    var user_info =  app.getUserInfo();
                    app.get("api/user/info", {user_id:user_info.user_id,fields:'store'},function(res){
                        that.store = res.data.store;
                    });
                    app.get("apiv2/wx_card/get", {
                      store_id : user_info.store_id,
                      type:'member_card',
                      limit:50
                    }, function (res) {
                      that.loading = 0;
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }

                      if(!res.data.list.length){
                        that.showAddMemberCard=1;
                      }
                      that.items = res.data.list;
                      that.getMemberCarddetail();
                       if(that.items.length===0){
                        that.nodata=1;
                      }else{
                        that.nodata=0;
                      }
                      
                    });
                }
            }

        }).$mount('#app');
    };
      
    module.exports = controller;
});
